// pages/edit/edit.js
var util = require('../../utils/util.js')
const db = wx.cloud.database()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    option: [
      { text: '请选择类型', value: 0 },
      { text: '清华缘', value: 1 },
      { text: '失物招领', value: 2 },
      { text: '二手交易', value: 3 },
      { text: '学习辅导', value: 4 },
      { text: '其他', value: 5 },
    ],
    tag: 0,
    title: '',
    desc: '',
    radio: '',
    radio2: '1',
    radio3: '1',
    price:'',
    fileID:'',
    fileList: [],
  },

  onChangetag(e){
    console.log('tag',e.detail)
    this.setData({
      tag: e.detail
    })
  },

  onChange2(event) {
    console.log(event.detail)
    this.setData({
      radio2: event.detail,
      radio: event.detail
    });
  },
  onClick2(event) {
    console.log(event.currentTarget.dataset)
    const { name } = event.currentTarget.dataset;
    this.setData({
      radio2: name,
      radio: name
    });
  },
  onChange3(event) {
    this.setData({
      radio3: event.detail,
      radio: event.detail
    });
  },
  onClick3(event) {
    const { name } = event.currentTarget.dataset;
    this.setData({
      radio3: name,
      radio: name
    });
  },
  
  afterRead(event) {
    const { file } = event.detail;
    console.log(event.detail)
    const filePaths = file.path
    
    let randString = Math.floor(Math.random() * 1000000).toString() + '.png'
    var timestamp = Date.parse(new Date())

    wx.cloud.uploadFile({
      cloudPath: 'img-COT/myimg-' + timestamp + randString,
      filePath: filePaths,
      success: res => {
        console.log('打印fileID', res.fileID)
        wx.showToast({
          title: '添加图片成功',
        })
        this.setData({
          fileID: res.fileID
        })
        const { fileList = [] } = this.data;
        fileList.push({ ...file, url: res.fileID });
        this.setData({ fileList });
      },
      fail: console.error
    })
  },

  onSubmit: function () {
    db.collection('Issues').add({
      data: {
        title: this.data.title,
        desc: this.data.desc,
        tags: this.data.tag,
        price: this.data.price,
        radio: Number(this.data.radio),
        img:this.data.fileID,
        date: util.formatTime(new Date()),
        timestamp: Date.parse(new Date())
      }, success: ret => {
        console.log(ret)
        wx.showToast({
          title: '发布成功',
        })
      }
    })    
  },

  onTitle(event) {
    this.setData({
      title: event.detail.value
    })
  },
  onDesc(event) {
    console.log(event.detail.value)
    this.setData({
      desc: event.detail.value
    })
  },
  onPrice(event) {
    this.setData({
      price: event.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})